﻿@model PagedList.IPagedList<Models.DataModel.Contribution>
@using PagedList.Mvc;

@{
    ViewBag.Title = "Contribution";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Contribution</h2>
<div class="form-inline">
    <div class="form-group">
        <input type="text" class="form-control" id="searchtext" name="searchtext" placeholder="Please input value" value="@ViewBag.SearchText">
    </div>
    <button type="button" onclick="search();" class="btn btn-default">Search</button>
    <button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Create", "Contribution")'">Add New ContributionRate</button>
</div>  
<br />
<div class="table-responsive" id="list">
    <table class="table table-bordered table-hover table-condensed">
        <thead>
            <tr>
                <th class="form-inline" style="width:100px; ">
                    <input id="ChkCheckFull" name="delall" type="checkbox" value="" />
                    <button type="submit" class="glyphicon glyphicon-trash btn-sm" value="" onclick="return DeleteContributionRateSelected()" style="color:red; border:none; background:none"></button>
                </th>
                <th>
                    @Html.Label("CompanyNumber")
                </th>
                <th>
                    @Html.Label("CompanyName")
                </th>
                <th>
                    @Html.Label("PortNumber")
                </th>
                <th>
                    @Html.Label("PortName")
                </th>
                <th>
                    @Html.Label("LocalNumber")
                </th>
                <th>
                    @Html.Label("HourType")
                </th>
                <th>
                    @Html.Label("LaborType")
                </th>
                <th>
                    @Html.Label("CreatedOn")
                </th>
                <th>
                    @Html.Label("DateOfContribution")
                </th>
                <th>
                    @Html.Label("NumberOfHours")
                </th>
                <th>
                    @Html.Label("CreatedByName")
                </th>
               
                <th>
                    @Html.Label("TransactionCode")
                </th>
                <th>
                    @Html.Label("Comment")
                </th>
                <th>
                    @Html.Label("Options")
                </th>
            </tr>
        </thead>
        <tbody class="tbody" id="tbody"></tbody>
    </table>
</div>
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("Index",
              new { page, searchtext = ViewBag.SearchText }))

<script>
    $(document).ready(function () {
        LoadData();
    });
    function LoadData(searchtext) {
        var i = 0;
        $.ajax({
            url: "/Contribution/LoadData",
            type: "GET",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ searchtext: searchtext }),
            dataType: "json",
            success: function (result) {
                var html = '';
                $.each(result, function (key, item) {
                    ++i;
                    var style = i % 2 == 0 ? "even" : "odd";
                    html += '<tr class=' + style + '>';
                    html += '<td> <input type="checkbox" class="checkCust" name="ContributionId" id="' + item.ContributionId + '" value=' + item.ContributionId + ' /> </td>'
                    html += '<td>' + item.CompanyNumber + '</td>';
                    html += '<td class="fcode">' + item.CompanyName + '</td>';
                    html += '<td>' + item.PortNumber + '</td>';
                    html += '<td>' + item.PortName + '</td>';
                    html += '<td>' + item.LocalNumber + '</td>';
                    html += '<td>' + item.HourType + '</td>';
                    html += '<td>' + item.LaborType + '</td>';
                    html += '<td>' + FormatJsonDate(item.CreatedOn) + '</td>';
                    html += '<td>' + FormatJsonDate(item.DateOfContribution) + '</td>';
                    html += '<td>' + item.NumberOfHours + '</td>';
                    html += '<td>' + item.CreatedByName + '</td>';
                    html += '<td>' + item.TransactionCode + '</td>';
                    html += '<td>' + item.Comment + '</td>';
                    html += '<td> <a href="/Contribution/Details/' + item.ContributionId + '">Detail</a> | <a href="javascript:DeleteContribution(\'' + item.ContributionId + '\');">Delete</a></td>';
                    html += '</tr>';
                });
                $('#list tbody').html(html);
            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
    }
   
    /// tim kiem tu dong
    $(document).ready(function () {
        $.ajax({
            url: "/Contribution/TextSearch",
            type: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                var option = {
                    source: result
                };
                $('#searchtext').typeahead(option);
           }
        });

    });
    // check auto
    $("#ChkCheckFull").click(function () {
        if ($("#ChkCheckFull").is(':checked') == true) {
            $(".checkCust").prop('checked', true);
        }
        else {
            $(".checkCust").prop('checked', false);
        }
    });
    // conver datetime
    function FormatJsonDate(jsonDt) {
        var MIN_DATE = -62135578800000; // const

        var date = new Date(parseInt(jsonDt.substr(6, jsonDt.length - 8)));
        return date.toString() == new Date(MIN_DATE).toString() ? "" : (date.getMonth() + 1) + "\/" + date.getDate() + "\/" + date.getFullYear();
    }
    /// xoa nhieu dong
    function DeleteContributionRateSelected() {
        var cf = confirm('Are you sure you want to delete rows selected?');
        var lstData = [];
        if (cf == true) {
            $('.checkCust').each(function () {
                var sThisVal = (this.checked ? "1" : "0");
                if (sThisVal == 1) {
                    lstData.push($(this).attr('id'));
                }
            });

            // call controller and tranfer data
            $.ajax({
                url: "/Contribution/DeleteSelectedContribution/" + lstData,
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                async: true,
                data: JSON.stringify({ contributiondelete: lstData }),
                success: function (result) {
                    LoadData();
                }
            });
        }
    }
    function DeleteContribution(ContributionId) {
        var cf = confirm('Are you sure you want to delete this row?');
        if (cf) {
            $.ajax({
                url: "/Contribution/Delete/" + ContributionId,
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                async: true,
                success: function (result) {
                    LoadData();
                },
                error: function (errormessage) {
                    alert(errormessage.responseText);
                }
            });
        }
    }
    $("#searchtext").change(function search() {
        //do whatever you need to do on actual change of the value of the input field
        var table = $("table tbody");

        table.find('tr').each(function (i, el) {
            var $tds = $(this).find('td'),
                fundcode = $tds.eq(1).text();
            fundcode += $tds.eq(2).text()+" ";
            fundcode += $tds.eq(3).text() + " ";
            fundcode += $tds.eq(4).text() + " ";
            if (fundcode.search($("#searchtext").val()) == -1) {
                $(this).hide();
            } else $(this).show();
        });
    });

</script>
